﻿using System.IO;
using DL.Tools;
using UnityEngine;

/// <summary>
/// 简单的文本文件操作辅助类
/// </summary>
public class TextFileHelper
{
	/// <summary>
	/// 配置文件的拓展名
	/// </summary>
	private static readonly string[] ConfigFileExt = new string[]
	{
		".txt",
		".xml",
		".json",
		".lua",
		"",
	};

	/// <summary>
	/// 保存文件
	/// </summary>
	public static bool SaveTextToFile(string _filePath, string _text)
	{
		var res = false;
		FileStream _fileStream = null;
		TextWriter _writer = null;
		var folder = Path.GetDirectoryName(_filePath);
		if (!Directory.Exists(folder))
		{
			Directory.CreateDirectory(folder);
			Debug.LogFormat("文件夹({0})不存在, 创建了文件夹!!", folder);
		}

		try
		{
			_fileStream = File.Open(_filePath, FileMode.OpenOrCreate);
			_fileStream.SetLength(0);
			_writer = new StreamWriter(_fileStream);
			_writer.Write(_text);
			if (_writer != null)
			{
				_writer.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}

			res = true;
		}
		catch (System.Exception e)
		{
			Debugger.EO_Log(e);
			Debug.LogError("SaveTextToFile ERROR:" + _filePath);
			if (_writer != null)
			{
				_writer.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}

		return res;
	}


	/// <summary>
	/// 将文本添加写入文件
	/// </summary>
	public static bool AppendTextToFile(string _filePath, string _text)
	{
		var res = false;
		FileStream _fileStream = null;
		TextWriter _writer = null;
		try
		{
			_fileStream = File.Open(_filePath, FileMode.OpenOrCreate);
			_fileStream.Position = _fileStream.Length;
			_writer = new StreamWriter(_fileStream);
			_writer.Write(_text);
			if (_writer != null)
			{
				_writer.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}

			res = true;
		}
		catch (System.Exception e)
		{
			Debugger.EO_Log(e);
			Debug.LogError("SaveTextToFile ERROR:" + _filePath);
			if (_writer != null)
			{
				_writer.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}

		return res;
	}

	/// <summary>
	/// 读取文件
	/// </summary>
	public static string ReadTextFromFile(string _filePath)
	{
		string res = null;
		FileStream _fileStream = null;
		TextReader _reader = null;
		try
		{
			_fileStream = File.Open(_filePath, FileMode.OpenOrCreate);
			_reader = new StreamReader(_fileStream);
			res = _reader.ReadToEnd();
			if (_reader != null)
			{
				_reader.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}
		catch (System.Exception e)
		{
			Debugger.EO_Log(e);
			Debug.LogError("ReadTextToFile ERROR:" + _filePath);
			if (_reader != null)
			{
				_reader.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}

		return res;
	}

	/// <summary>
	/// 读取文件
	/// </summary>
	public static string ReadTextFromFileIgnoreSuffix(string _filePath)
	{
		string res = null;
		FileStream _fileStream = null;
		TextReader _reader = null;
		try
		{
			foreach (var it in ConfigFileExt)
			{
				var modifiedPath = _filePath + it;
				if (File.Exists(modifiedPath))
				{
					_fileStream = File.Open(modifiedPath, FileMode.OpenOrCreate);
					break;
				}
			}

			if (_fileStream != null)
			{
				_reader = new StreamReader(_fileStream);
				res = _reader.ReadToEnd();
			}

			if (_reader != null)
			{
				_reader.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}
		catch (System.Exception e)
		{
			Debug.LogException(e);
			if (_reader != null)
			{
				_reader.Close();
			}

			if (_fileStream != null)
			{
				_fileStream.Close();
			}
		}

		return res;
	}

	/// <summary>
	/// 判断文件是否存在
	/// </summary>
	public static bool IsFileExist(string filePath)
	{
		return File.Exists(filePath);
	}

	/// <summary>
	/// 判断文件是否存在 (不考虑文件后缀)
	/// </summary>
	public static bool IsFileExistIgnoreSuffix(string filePath)
	{
		var fined = false;
		foreach (var it in ConfigFileExt)
		{
			var modifiedPath = filePath + it;
			if (File.Exists(modifiedPath))
			{
				fined = true;
				break;
			}
		}

		return fined;
	}
}